package com.lph.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import com.alibaba.fastjson.JSON;
import com.lph.model.Files;
import com.lph.model.Msg;
import com.lph.model.Notices;
import com.lph.service.BaseServiceI;
import com.lph.util.FileIOUtil;
@Controller
@RequestMapping("/nc")
public class NoticesController extends BaseController<Notices>{
		@Resource(name="noticesService")
		private BaseServiceI<Notices> nbsi;
		
		@Resource(name="filesService")
		private BaseServiceI<Files> fbsi;
		
		@RequestMapping("/no.coc")
		public String notices(){
			return "/notices/index";
			
		}
		//文件的上传
		@Override
		public Msg insert(Notices t, @RequestParam(required=false,value="files")MultipartFile[] files) {
		Msg m=nbsi.insert(t);
			if(m.isIs()){
				try {
				List<Files> list=	FileIOUtil.saveFile(files, "t_coc_notices", t.getId());
				for (Files file : list) {
					fbsi.insert(file);
				}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		return m;
		}
		//首页的通知
		@RequestMapping("/findNotice.coc")
		@ResponseBody
		public List<Notices> findNotice(){
			List<Notices> list=nbsi.find(new Notices());
			List< Notices> nli=new ArrayList<Notices>();
			for (Notices notices : list) {
			/*	if(notices.getEndDate().getTime()<System.currentTimeMillis()){
					continue;
				}*/
				nli.add(notices);
				if(nli.size()==5) break;		
				
			}
			return nli;
			
		}
		
		@Override
		public List<Notices> find(Notices t) {
		return nbsi.find(t);
		}
		
		@RequestMapping("/insertF.coc")
		@ResponseBody
		public Msg insert(Notices t) {
		return nbsi.insert(t);
		}
		
		@Override
		public Msg edit(Notices t, MultipartFile[] files) {
		return nbsi.edit(t);
		}
		@Override
		public Msg remove(Integer id) {
		// TODO Auto-generated method stub
		return nbsi.remove(id);
		}
		
		@RequestMapping("/showNotices")
		public String showNotices(Integer index,HttpServletRequest req) {
			List<Notices> list=nbsi.find(new Notices());
			
			Notices n=list.get(index);
			req.setAttribute("notices", list.size());
			req.setAttribute("n", n);
			req.setAttribute("i", index);
			return "/notices/showNotices";
			

		}
		@RequestMapping("/lookNotice/{rid}")
		public String lookNotice(HttpServletRequest req,@PathVariable(value="nid") Integer id){
			
				Notices n=new Notices();
				n.setId(id);
				n=nbsi.get(n);
				req.setAttribute("n", n);
			return "/notices/lookNotice";
			
		}
		
		@RequestMapping("/getNotices")
		@ResponseBody
		public Notices getNotices(Integer index) {
			List<Notices> list=nbsi.find(new Notices());
			
		Notices n=list.get(index);
		return n;
		}
}
